﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace JNCY.WinAutoComplate
{
   public class DataHandle
    {
        public DataTable Source { get; set; }

        public DataTable Result { get; set; }

        public void Handle(string txt)
        {
            Result = new DataTable();
            Result = Source.Clone();

            List<string> list = new List<string>();
            List<string> _list = new List<string>();
            if (Source != null)
            {
                if (Source.Rows.Count >0)
                {
                    //DataRow item = Source.Rows[0];
                    
                    for (int i = 0; i < Source.Columns.Count; i++)
                    {
                        list.Add(string.Format("{0} like '%{1}%'", Source.Columns[i].Caption, txt.Trim()));
                        _list.Add(Source.Columns[i].Caption);
                    }
                }
            }

            if (list.Count == 0)
            {
                return;
            }

            string search=string.Join(" or ", list);

            string searchs = string.Join("+", _list);

            DataRow[] rows= Source.Select(searchs+" like '%"+txt+"%'");

            if (rows.Count() != 0)
            {
                //Result.Rows.Add(Source.Rows[0]);
                foreach (var temrows in rows)
                {
                    Result.ImportRow(temrows);
                }
            }
        }
    }
}
